package cn.dhnapplets.service.impl;


import cn.dhnapplets.entity.*;
import cn.dhnapplets.mapper.BillMapper;
import cn.dhnapplets.service.BillService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author 邱文彬
 * @Date 2021/11/23 9:15
 * @Version 1.0
 */

@Service
public class BillServiceImpl implements BillService {


    @Autowired
    private BillMapper billMapper;


    @Override
    public Result getQuantityOfDeliveryByOpenId(String openId, String[] category) {
        System.out.println(openId + "111111111111111111111111111111");
        Map<String, Double> doubleMap = new HashMap<>();
        for (String productType : category) {
            System.out.println(openId);
            Double num1 = billMapper.queryOrderByOpenId(openId, productType);
            Double num2 = billMapper.queryBillHorsemandByOrderId(openId, productType);
            if(num2 == null){
                num2 = 0.00;
            }
            if(num1 == null){
                num1 = 0.00;
            }
            doubleMap.put(productType, num1 - num2);
        }


        return new Result(0, "操作成功", doubleMap);
    }

    @Override
    public Result getQuotaOrderByOpenId(String openId, String[] category) {

        List<Order> orderList = billMapper.queryOrderNumberByOpenId(openId);

        Map<String, List<TotalOrderState>> stringListMap = new HashMap<>();
        int num = 0;

        int num2 = 0;

        for (int x = 0; x < category.length; x++) {
            List<TotalOrderState> totalOrderStateList = new ArrayList<>();
            List<OrderItem> orderItemList = billMapper.queryOrderNumberItemByOpenId(openId, category[x]);
            List<BillHoresmand> billHoresmandList = billMapper.queryBullByOpenId(openId, category[x]);

            for (OrderItem orderItem : orderItemList) {
                OrderState orderState = new OrderState();
                for (Order order : orderList) {
                    TotalOrderState totalOrderState = new TotalOrderState();
                    if (orderItem.getOrderId().equals(order.getId())) {
                        String orderNumber = order.getOrderNumber();
                        totalOrderState.setOrderNumber(orderNumber);
                        orderState.setState(0);
                        orderState.setCategory(orderItem.getProductType());
                        orderState.setWeight(orderItem.getFinallyWeight());
                        orderState.setOrderId(orderItem.getOrderId());
                        for (BillHoresmand billHoresmand : billHoresmandList) {
                            if (orderItem.getOrderId().equals(billHoresmand.getOrderId())) {
                                orderState.setState(2);
                            }
                        }
                        totalOrderState.setOrderState(orderState);
                        totalOrderStateList.add(num++, totalOrderState);
                        continue;
                    }

                }
                num = 0;
            }
            System.out.println(num2++ + category[x]);
            stringListMap.put(category[x], totalOrderStateList);


        }


        return new Result(0, "操作成功", stringListMap);
    }

    @Override
    public Result indert(BillHoresmandNu billHoresmand) {
        BillHoresmand billHoresmandnu = new BillHoresmand();

        billHoresmandnu.setDeliveryTime(new Date());
        billHoresmandnu.setStatus(2);
        billHoresmandnu.setBillOrderMerchant(billHoresmand.getBillOrderMerchant());
        billHoresmandnu.setOrderId(billHoresmand.getOrderId());
        billHoresmandnu.setOrderWeight(billHoresmand.getOrderWeight());
        billHoresmandnu.setProductType(billHoresmand.getProductType());
        billHoresmandnu.setOrderWeight(billHoresmand.getOrderWeight());
        billHoresmandnu.setOpenId(billHoresmand.getOpenId());
        billHoresmandnu.setProductTypeAll(billHoresmand.getProductTypeAll());

        Integer insert = billMapper.insert(billHoresmandnu);
        if (insert > 0) {
            return new Result(0, "操作成功", null);
        }
        return new Result(10001, "操作失败", null);


    }


//    根据订单ID查询绑定的所有订单
    @Override
    public Result getAll(String id) {

        return  null;
    }
}
